<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\User
 *
 * @property integer $id
 * @property string $username
 * @property string $email
 * @property string $phone
 * @property string $password
 * @property string $reg_ip
 * @property string $reg_time
 * @property string $login_ip
 * @property string $login_time
 * @property integer $login_count
 * @property integer $role
 * @property string $remember_token
 * @property string $deleted_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property-read \App\UserInfo $userInfo
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Project[] $project
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\UserWork[] $userWork
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\UserCreate[] $userCreate
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\UserTeams[] $userTeams
 * @property-read \App\Investor $investor
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\InvestLog[] $investLog
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Message[] $message
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Investor[] $userFocusInvestor
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Project[] $userFocusProject
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\InvestOrg[] $userFocusInvestOrg
 * @method static \Illuminate\Database\Query\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereUsername($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User wherePhone($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereRegIp($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereRegTime($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereLoginIp($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereLoginTime($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereLoginCount($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereRole($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereRememberToken($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\User whereUpdatedAt($value)
 * @property integer $status
 * @method static \Illuminate\Database\Query\Builder|\App\User whereStatus($value)
 * @property integer $is_power
 * @method static \Illuminate\Database\Query\Builder|\App\User whereIsPower($value)
 */
class User extends Model implements AuthenticatableContract{

    use Authenticatable;

	//表名
    protected $table = "users" ;
    protected $hidden = ['password','remember_token'];
    use SoftDeletes;

    /** 关联用户详细信息表 */
    public function userInfo()
    {
        return $this->hasOne('App\UserInfo' , 'user_id') ;
    }

    /** 关联项目表 */
    public function project()
    {
        return $this->hasMany('App\Project' , 'user_id') ;
    }

    /** 关联用户工作经历表 */
    public function userWork()
    {
        return $this->hasMany('App\UserWork' , 'user_id') ;
    }

    /** 关联用户创业经历表 */
    public function userCreate()
    {
        return $this->hasMany('App\UserCreate' , 'user_id') ;
    }

    /** 关联用户团队成员表 */
    public function userTeams()
    {
        return $this->hasMany('App\UserTeams' , 'user_id') ;
    }

    /** 关联投资人信息表 */
    public function investor()
    {
        return $this->hasOne('App\Investor' , 'user_id') ;
    }

    /** 关联投资经历表 */
    public function investLog()
    {
        return $this->hasMany('App\InvestLog' , 'user_id') ;
    }

    /** 关联消息表 */
    public function message()
    {
        return $this->hasMany('App\Message','to_user_id');
    }

    /** 关联中间表-用户关注投资人 */
    public function userFocusInvestor()
    {
        return $this->belongsToMany('App\Investor','focus_investor','user_id','investor_id');
    }

    /** 关联中间表-用户关注项目 */
    public function userFocusProject()
    {
        return $this->belongsToMany('App\Project','focus_project','user_id','project_id');
    }

    /** 关联中间表-用户关注投资机构 */
    public function userFocusInvestOrg()
    {
        return $this->belongsToMany('App\InvestOrg','focus_invest_org','user_id','org_id');
    }

    /**
     * 判断用户是否关注了该项目
     * @param $projectId
     * @return mixed
     */
    public function isFocusProject($projectId)
    {
        return $this->userFocusProject()->wherePivot('project_id','=',$projectId)->exists();
    }

    /**
     * 判断用户是否关注了该投资人
     * @param $investorId
     * @return mixed
     */
    public function isFocusInvestor($investorId)
    {
        return $this->userFocusInvestor()->wherePivot('investor_id','=',$investorId)->exists();
    }

    /**
     * 判断用户是否关注了该投资机构
     * @param $investorgId
     * @return mixed
     */
    public function isFocusInvestOrg($investorgId)
    {
        return $this->userFocusInvestOrg()->wherePivot('org_id','=',$investorgId)->exists();
    }

    /**
     * 通过用户的标识来获取用户的工作经历
     *
     * @param $UserId
     * @return string       返回工作经历信息
     */
    public function getUserWorkByUserId($UserId)
    {
        $UserWork = UserWork::whereUserId($UserId)->orderBy('created_at' , 'desc')->first();
        if($UserWork)
            return $UserWork->company.$UserWork->position;

    }

    public function getUserCreateByUserId($userId)
    {
        $userCreate = UserCreate::whereUserId($userId)->orderBy('caeated_at','desc')->first();
        if($userCreate)
            return $userCreate ;
        else
            return '';
    }


    /**
     * 通过用户标识来获取 最新的创业项目
     * @param $UserId
     * @return mixed
     */
    public function getProjectByUserId($UserId)
    {
        $Project = Project::whereUserId($UserId)->orderBy('created_at' , 'desc')->first();
        if($Project)
            return $Project;
        else
            return new Project();
    }

    /**
     * 通过项目标识获取  创业项目的角色信息
     * @param $projectId
     * @return null
     */
    public function getPositionByProjectId($projectId)
    {
        $UserCreate = UserCreate::whereProjectId($projectId)->orderBy('created_at','desc')->first();
        if($UserCreate)
            return $UserCreate->position;
        else
            return null;
    }

    /**
     * 通过创业者项目标识获取  创业项目所在领域
     * @param $projectId
     * @return null|string
     */
    public function getFieldsByProjectId($projectId)
    {
        $projects = Project::find($projectId)->project_x_fields()->getResults();

        $fields = array();
        if($projects){
            foreach($projects as $project){
                $fields[] = $project['field'];
            }
        }
        return $fields;
    }

    /**
     * 通过用户标识获取投资人  投资领域
     * @param $investorId
     * @return array
     */
    public function getFieldsByUserId($userId)
    {
        /** @var  $investorId  获取投资人标识 */
        $investorId = Investor::whereUserId($userId)->pluck('id');
        $investors = Investor::find($investorId);
        if($investors){
            $fieldsByInvestors = Investor::find($investorId)->investor_x_fields()->getResults();
            $fields = array();
            if($fieldsByInvestors){
                foreach($fieldsByInvestors as $investor){
                    $fields[] = $investor['field'];
                }
                return $fields;
            }
            return false;
        }
        return false;
    }

    /**
     * 通过获取投资人标识获取 投资阶段
     * @param $userId
     * @return array
     */

    public function getPhaseByProjectId($projectId)
    {
        /** @var  $investorId  获取投资人标识 */
        $phaseId = Project::whereId($projectId)->pluck('phase_id');

        $phase = Phases::whereId($phaseId)->pluck('phase');
        if($phase)
            return $phase;
        else
            return '';
    }

    /**
     * 通过用户标识获取 投资项目
     * @param $userId
     * @return array
     */
    public function  getInvestProjectByUserId($userId)
    {
        /**通过用户标识 获取投资项目标识 project_id */
        $projectIds = InvestLog::whereUserId($userId)->whereStatus(1)->lists('project_id');
        $projects = array();
        if($projectIds) {
            foreach ($projectIds as $projectId) {
                $projects[] = Project::find($projectId);
            }
        }
        return $projects;
    }

    /**
     * 通过用户标识获取 投资金额范围
     * @param $userId
     * @return mixed|static
     */
    public function getInvestAreaByUserId($userId)
    {
        $investor = Investor::whereUserId($userId)->first();
        if($investor){
            $min = $investor->invest_num_small;
            $max = $investor->invest_num_big;
            return '￥'.$min.'-'.$max.'万元' ;
        }
        return '暂无数据';

    }

    /**
     * 用户为普通用户登录
     */
    public static function isUsers($userId)
    {
        if(User::whereId($userId)->whereIsPower(1)->exists())
            return true;
        else
            return false;
    }

    /**
     * 用户未管理员 或者超级管理员登录
     */
    public static function isAdmins($userId)
    {
        if(User::whereId($userId)->whereIn('is_power' ,[2,3])->exists())
            return true;
        else
            return false;
    }

    public function isMaster(){

    }

    public function isAdmin(){

    }


    /**
     * 通过用户标识 获取投资人的领域
     * @param $userId
     * @return array|\Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function isCheckedFields($userId)
    {
        $fields = array();
        if(Investor::whereUserId($userId)->exists()){
            $investor   = Investor::whereUserId($userId)->first();
            $fields     = $investor->investor_x_fields()->lists('field_id');
        }
        return $fields;
    }

    /**
     * 通过用户标识 获取投资人的投资阶段
     * @param $userId
     * @return array|\Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function isCheckedPhases($userId)
    {
        $phases = array();
        if(Investor::whereUserId($userId)->exists()){
            $investor   =   Investor::whereUserId($userId)->first();
            $phases     =   $investor->investor_x_phases()->lists('phase_id');
        }
        return $phases;
    }

}
